﻿/***
 * 
 *    Title: "ZGUIFW"UI框架项目
 *           主题： UI遮罩管理器     
 *    Description: 
 *           功能： XXXX
 *    Date: 2018
 *    Version: 0.1版本
 */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace ZGUIFW
{
	public class UIMaskMgr : MonoBehaviour
    {
		//使用单例模式来调用
		private static UIMaskMgr _instance = null;
		//UI根节点对象
		private GameObject _goCanvasRoot = null;
		//UI脚本根节点对象
		private Transform _transUIScriptNode = null;
		//顶层面板
		private GameObject _goTopPanel;
		//遮罩面板
		private GameObject _goMaskPanel;
		//UI摄像机
		private Camera _uiCamera;              
		//UI摄像机的层深
		private float _originalUICameraDepth;

		public static UIMaskMgr GetInstance(){
			return _instance ?? (_instance = new GameObject("_UIMaskMgr").AddComponent<UIMaskMgr>());
		}
		private void Awake()
		{
			_goCanvasRoot = GameObject.FindWithTag(SystemDefine.SYS_TAG_CANVAS);
			_transUIScriptNode = UnityHelper.FindTheChildNode(_goCanvasRoot, SystemDefine.SYS_CANVAS_SCRIPTMGR);
			UnityHelper.SetParent(_transUIScriptNode, this.gameObject.transform);
            //UI界面必须是场景内的最顶层  所以”顶层面板就是整个UI的canvas“
			_goTopPanel = _goCanvasRoot;
			//遮罩面板
			_goMaskPanel = UnityHelper.FindTheChildNode(_goCanvasRoot, "_UIMaskPanel").gameObject;
			//得到UI摄像机的层深
			_uiCamera = GameObject.FindWithTag("_TagUICamera").GetComponent<Camera>();
			if(_uiCamera == null)
			{
				Debug.LogError("Camera is null");
				return;
			}
			_originalUICameraDepth = _uiCamera.depth;         
		}
        /// <summary>
        /// Sets the mask panel.
        /// </summary>
        /// <param name="goDisplay">Go display.</param>
        /// <param name="lucenyType">Luceny type.</param>
		public void SetMaskPanel(GameObject goDisplay, UIFormLucenyType lucenyType = UIFormLucenyType.Lucency)
		{
			//整体的UICanvas下
			_goTopPanel.transform.SetAsLastSibling();
			//启动遮罩窗体以及设置透明度
			switch(lucenyType)
			{
				case UIFormLucenyType.Lucency:
					_goMaskPanel.SetActive(true);
					_goMaskPanel.GetComponent<Image>().color =
						            SystemDefine.SYS_UIMASK_LUCENCY_COLOR_RGBA;
					break;
				case UIFormLucenyType.Translucence:
					_goMaskPanel.SetActive(true);
					_goMaskPanel.GetComponent<Image>().color =
						            SystemDefine.SYS_UIMASK_TRANSLUCENCE_COLOR_RGBA;
					break;
				case UIFormLucenyType.ImPenetrable:
					_goMaskPanel.SetActive(true);
					_goMaskPanel.GetComponent<Image>().color =
						            SystemDefine.SYS_UIMASK_IMPENTRABLE_COLOR_RGBA;
					break;
				case UIFormLucenyType.Penetrate:
					if (_goMaskPanel.activeInHierarchy) _goMaskPanel.SetActive(false);
					break;
			}
			//遮罩窗体下移
			_goMaskPanel.transform.SetAsLastSibling();
			//显示窗体的下移
			goDisplay.transform.SetAsLastSibling();
			//增加当前UI摄像机的层深
			if (_uiCamera == null) return;
			_uiCamera.depth = _uiCamera.depth + 100;
		}

        /// <summary>
        /// Cancels the mask panel.
        /// </summary>
		public void CancelMaskPanel()
		{
			//整体的UICanvas上移
			_goTopPanel.transform.SetAsFirstSibling();
			//禁用遮罩窗体
			if (_goMaskPanel.activeInHierarchy) _goMaskPanel.SetActive(false);
            //恢复当前UI摄像机的层深
            if (_uiCamera == null) return;
			_uiCamera.depth = _originalUICameraDepth;
		}
	}
}

